﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace MyHospital.BLL
{
    public class EmailSender
    {
        //配置节获取相应的键值
        private string mailFromAddress = ConfigurationManager.AppSettings["MailFromAddress"];
        private bool useSsl = bool.Parse(ConfigurationManager.AppSettings["UseSsl"]);
        private string userName= ConfigurationManager.AppSettings["UserName"];
        private string password = ConfigurationManager.AppSettings["Password"];
        private string serverName = ConfigurationManager.AppSettings["ServerName"];
        private int serverPort = int.Parse(ConfigurationManager.AppSettings["ServerPort"]);
        private string findPassword;//重置后密码
        private string mailToAddress = "";//收件人邮箱
        public EmailSender(string address,string pwd)//构造函数获取需修改用户邮箱及密码
        {
            mailToAddress = address;
            findPassword = pwd;
        }
        public void Send()
        {
            using(var smtpClient=new SmtpClient())
            {
                smtpClient.EnableSsl = useSsl;
                smtpClient.Host = serverName;
                smtpClient.Port = serverPort;
                smtpClient.Credentials = new NetworkCredential(userName,password);
                string body = "您登录的网上挂号系统的密码已重置为:" + findPassword;
                MailMessage mailMessage = new MailMessage(
                               mailFromAddress,        //发件人邮箱
                               mailToAddress,          //收件人邮箱
                               "网上挂号系统用户密码重置",//邮件主题
                               body);                  //右键内容
                smtpClient.Send(mailMessage);
            }
        }
    }
}
